//
//  DWTagList.m
//
//  Created by Dominic Wroblewski on 07/07/2012.
//  Copyright (c) 2012 Terracoding LTD. All rights reserved.
//

#import "DWTagList.h"
#import <QuartzCore/QuartzCore.h>
#import "RestaurantSearchViewController.h"
#import "HotSearchViewController.h"
#define CORNER_RADIUS 1.0f
#define LABEL_MARGIN 5.0f
#define BOTTOM_MARGIN 5.0f
#define FONT_SIZE 14.0f
#define HORIZONTAL_PADDING 10.0f
#define VERTICAL_PADDING 3.0f
#define BACKGROUND_COLOR [UIColor colorWithRed:0.93f green:0.83f blue:0.53f alpha:0.5f]
#define BACKGROUND_HOTCOLOR [UIColor colorWithRed:0.93f green:0.53f blue:0.53f alpha:1.0f]

#define TEXT_COLOR [UIColor blackColor]
#define TEXT_SHADOW_COLOR [UIColor whiteColor]
#define TEXT_SHADOW_OFFSET CGSizeMake(0.0f, 1.0f)
#define BORDER_COLOR [UIColor lightGrayColor].CGColor
#define BORDER_WIDTH 1.0f

@implementation DWTagList

@synthesize myview, textArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:myview];
    }
    return self;
}

- (void)setTags:(NSArray *)array
{
    if(array==nil || (NSNull*)array==[NSNull null])
    {
        return;
    }
    textArray = [[NSArray alloc] initWithArray:array];
    sizeFit = CGSizeZero;
    [self display];
}

- (void)setLabelBackgroundColor:(UIColor *)color
{
    lblBackgroundColor = color;
    [self display];
}

- (void)display
{
    for (UILabel *subview in [self subviews]) {
        [subview removeFromSuperview];
    }
    float totalHeight = 0;
    CGRect previousFrame = CGRectZero;
    BOOL gotPreviousFrame = NO;
    for (NSDictionary *dic in textArray) {
//        for (NSString *text in textArray) {
        NSString *text = [dic objectForKey:@"areaName"];
        CGSize textSize = CGSizeMake(48, 20);//[text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(self.frame.size.width, 1500) lineBreakMode:UILineBreakModeWordWrap];
        textSize.width += HORIZONTAL_PADDING*2;
        textSize.height += VERTICAL_PADDING*2;
        UIButton *label = nil;
        if (!gotPreviousFrame) {
            label = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)];
            totalHeight = textSize.height;
        } else {
            CGRect newRect = CGRectZero;
            if (previousFrame.origin.x + previousFrame.size.width + textSize.width + LABEL_MARGIN > self.frame.size.width) {
                newRect.origin = CGPointMake(0, previousFrame.origin.y + textSize.height + BOTTOM_MARGIN);
                totalHeight += textSize.height + BOTTOM_MARGIN;
            } else {
                newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);
            }
            newRect.size = textSize;
            label = [[UIButton alloc] initWithFrame:newRect];
        }
        previousFrame = label.frame;
        gotPreviousFrame = YES;
//        [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
        [label.titleLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
        [label  setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        label.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
        if (!lblBackgroundColor) {
            [label setBackgroundColor:BACKGROUND_COLOR];
            
        } else {
            [label setBackgroundColor:lblBackgroundColor];
        }
        [label setTitle:text forState:UIControlStateNormal];
        label.tag = [[dic objectForKey:@"areaID"] intValue];
        [label setBackgroundImage:[self imageWithColor:BACKGROUND_HOTCOLOR size:label.frame.size] forState:UIControlStateHighlighted];
//        [label setBackgroundImage:[UIImage imageNamed:@"red.png"] forState:UIControlStateHighlighted];
//        [label setTextColor:TEXT_COLOR];
//        [label setText:text];
//        [label setTextAlignment:UITextAlignmentCenter];
//        [label setShadowColor:TEXT_SHADOW_COLOR];
//        [label setShadowOffset:TEXT_SHADOW_OFFSET];
//        [label.layer setMasksToBounds:YES];
        [label.layer setCornerRadius:CORNER_RADIUS];
        [label.layer setBorderColor:BORDER_COLOR];
//        [label.layer setBorderWidth: BORDER_WIDTH];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(announceLabelTaped:)];
        label.userInteractionEnabled = true;
        [label addGestureRecognizer:tapGesture];
        [self addSubview:label];
        
    }
    sizeFit = CGSizeMake(self.frame.size.width, totalHeight + 1.0f);
}

- (CGSize)fittedSize
{
    return sizeFit;
}
- (void)announceLabelTaped:(id)sender { UITapGestureRecognizer *gesture = (UITapGestureRecognizer *)sender;
    UIButton *label = (UIButton*)gesture.view;
    //判断手势状态是否结束
    if (gesture.state == UIGestureRecognizerStateEnded) {
        //获取label的tag值
        int tag = [gesture.view tag];

        HotSearchViewController  *view = [[HotSearchViewController alloc] initWithNibName:@"HotSearchViewController" bundle:nil];
        view.areaId = tag;
        view.areaName = [[label titleForState:UIControlStateNormal]copy];
        view.hidesBottomBarWhenPushed=YES;
        [[self viewController] .navigationController pushViewController:view animated:YES];
//        //	[view release];

    //获取label
//        NSLog(@"%@",[label titleForState:UIControlStateNormal]);
//        NSLog(@"%d",tag);

    }else {
        [label setBackgroundColor:[UIColor clearColor]];

    }
}
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    view.backgroundColor = color;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

@end
